package com.xwd.web.student;


import com.xwd.domain.Student;
import com.xwd.service.StudentService;
import com.xwd.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/AddStudent")
public class AddStudentServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接受参数
        Student student = new Student();
        student.setStuName(req.getParameter("stuName"));
        student.setStuNo(req.getParameter("stuNo"));
        student.setSex(Integer.parseInt(req.getParameter("sex")));
        student.setGid(Integer.parseInt(req.getParameter("gid")));
        student.setPhone(req.getParameter("phone"));
        student.setEmail(req.getParameter("email"));
        student.setRegistered(req.getParameter("registered"));
        student.setAddress(req.getParameter("address"));
        student.setProfession(req.getParameter("profession"));
        student.setIdNumber(req.getParameter("idNumber"));
        student.setPolitics(req.getParameter("politics"));
        student.setIntroduction(req.getParameter("introduction"));

        System.out.println(student);
        //调用service方法
        StudentService service = new StudentServiceImpl();
        Boolean exist = service.findStuNoExist(req.getParameter("stuNo"));

        int i = service.insertStu(student);
        PrintWriter writer = resp.getWriter();
        if(i>0 && !exist){
            writer.println("<script>location.href='/getStudetList'; alert('添加成功'); </script>");
        }else {
            writer.println("<script>location.href='/getClassTotal'; alert('添加失败，请重试'); </script>");
        }
    }
}
